import { request, getImageUrl } from '/utils';
import { TradePayResultCode } from '/constants';
Page({
    data: {
        eatingPattern: [{
                icon: '/assets/imgs/eatIn.png',
                name: '堂食',
                des: '店内食用',
                selected: true
            },
            {
                icon: '/assets/imgs/takeout.png',
                name: '打包',
                des: '店内取餐',
                selected: false
            },
        ],
        cartList: [],
        cartListStock: 0,
        cartListTotalPrice: '0.00',
        cartListTotalOriginalPrice: '0.00'
    },
    onLoad() {
        my.setCanPullDown({ canPullDown: false });
        this.getCartList();
    },
    tapWay({ target: { dataset: { index } } }) {
        const { eatingPattern } = this.data;
        eatingPattern.forEach((p, pIndex) => {
            p.selected = index === pIndex;
        });
        this.setData({
            eatingPattern
        });
    },
    async getCartList() {
        const { code, data: cartList } = await request.get('/miniapp/shoppingCart/getCartList');
        if (code !== 200) return;
        const cartListStock = cartList.reduce((pre, item) => pre + item.amount, 0);
        const cartListTotalPrice = (cartList.reduce((pre, item) => pre + item.sku.price * item.amount, 0) / 100).toFixed(2);
        const cartListTotalOriginalPrice = (cartList.reduce((pre, item) => pre + item.sku.originalPrice * item.amount, 0) / 100)
            .toFixed(2);
        this.setData({
            cartList: cartList.map(({ goods, sku, ...item }) => ({
                ...item,
                goods: {
                    ...goods,
                    cover: getImageUrl(goods
                        .cover)
                },
                sku: {
                    ...sku,
                    images: sku.images.map(item => getImageUrl(item)),
                    price: (sku.price * item
                        .amount / 100).toFixed(2),
                    originalPrice: (sku.originalPrice * item.amount / 100)
                        .toFixed(2)
                }
            })),
            cartListStock,
            cartListTotalPrice,
            cartListTotalOriginalPrice
        });
    },
    async handlePay() {
        my.showLoading({ content: '加载中', mask: true });
        try {
            const eatingPattern = this.data.eatingPattern.findIndex(item => item.selected) + 1;
            const { code, data } = await request.post('/miniapp/order/buy', { eatingPattern });
            if (code !== 200) return my.hideLoading();
            const { alipayTradeNo } = data;
            my.tradePay({
                tradeNO: alipayTradeNo,
                success(data) {
                    // const { resultCode } = data;
                    // const { Success, Unknown, Processing, Cancel } = TradePayResultCode;
                    my.redirectTo({ url: `/pages/orderDetail/orderDetail?alipayTradeNo=${alipayTradeNo}` });
                },
                complete: () => {
                    my.hideLoading();
                }
            });
        } catch (e) {
            my.hideLoading();
        }
    }
});